#!/usr/bin/env python
#-*- coding:utf-8 -*-

# 需要安装requests  pip install requests

import requests

#web请求辅助类
class webUtil(object):

    #get数据访问
    #url 访问路径
    #data 参数dict集合
    #cookie cookie值
    #encoding 编码模式
    #timeout 请求超时时间（单位秒）
    #返回response ,可以根据r.text获取相应内容，根据r.json()获取json对象，根据status_code 获取响应码
    @staticmethod
    def httpGet(url,data={},cookie=None,encoding='utf-8',timeout=60*1000,useragent=None,cert=None,verify=False):
        if useragent==None:
            useragent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"
        
        headers={'user-agent':useragent,
        'connection':'keep-alive',
        'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'}

        r=requests.get(url,params=data,headers=headers,cookies=cookie,timeout=timeout,verify=verify,cert=cert)
        r.encoding=encoding
        return r
        
    #post数据访问
    #url 访问路径
    #data 参数dict集合
    #cookie cookie值
    #encoding 编码模式
    #timeout 请求超时时间（单位秒）
    #stream 文件流
    #cert 证书
    #verify SSL证书验证
    #返回response ,可以根据r.text获取相应内容，根据r.json()获取json对象，根据status_code 获取响应码   
    @staticmethod
    def httpPost(url,data={},cookie=None,encoding='utf-8',timeout=60*1000,useragent=None,stream=None,cert=None,verify=False):
        if useragent==None:
            useragent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"
        
        headers={'user-agent':useragent,
        'connection':'keep-alive',
        'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'}

        r=requests.post(url,data=data,headers=headers,cookies=cookie,timeout=timeout,files=stream,verify=verify,cert=cert)
        r.encoding=encoding

        return r

    
        
